package com.qyy.springblog.controller;

import com.qyy.springblog.constants.Constant;
import com.qyy.springblog.model.Result;
import com.qyy.springblog.model.UserInfo;
import com.qyy.springblog.service.UserService;
import com.qyy.springblog.utils.JWTUtils;
import com.qyy.springblog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 登录接口
     * 校验用户名和密码是否正确
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public Result login(String userName, String password){
        //1.参数校验
        //2.对密码进行校验
        //3.如果校验成功，生成token
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return Result.fail("用户名或密码不能为空！");
        }
        UserInfo userInfo = userService.queryUserByName(userName);
        if(userInfo == null || userInfo.getId() <= 0){
            return Result.fail("用户不存在");
        }
//        if(!password.equals(userInfo.getPassword())){
//            return Result.fail("密码错误！");
//        }
        if(!SecurityUtils.verify(password,userInfo.getPassword())){
            return Result.fail("密码错误!");
        }
        //密码正确
        Map<String,Object> claim = new HashMap<>();
        claim.put(Constant.USER_CLAIM_Id,userInfo.getId());
        claim.put(Constant.USER_CLAIM_NAME,userInfo.getUserName());
        return Result.success(JWTUtils.gentToken(claim));
    }

    @RequestMapping("/register")
    public Result register(String userName, String password){
        //1.对参数进行判空，输入的用户名和密码不能为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return Result.fail("1");
        }
        UserInfo userInfo = userService.queryUserByName(userName);
        if(userInfo != null){
            return Result.fail("2");
        }
        if(userName.length() < 2 || userName.length() >13){
            return Result.fail("3");
        }
        if(password.length() < 3 || password.length() > 13){
            return Result.fail("4");
        }
        UserInfo user = new UserInfo();
        user.setUserName(userName);
        String p = SecurityUtils.encrypt(password);
        user.setPassword(p);
        Integer ret = userService.insert(user);

        return Result.success(user);
    }



    /**
     * 获取当前登录用户的信息
     */

    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request){
        //1.获取token，从token中获取Id
        //2.根据Id，获得用户信息
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JWTUtils.getUserIdFromToken(user_token);
        if(userId == null || userId<=0){
            return null;
        }
        UserInfo userInfo = userService.queryUserById(userId);
        userInfo.setPassword("");
        return userInfo;
    }



    /**
     * 根据博客Id，获取作者信息
     */
    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId){
        //1.根据博客Id，获取作者Id
        //2.根据作者Id，获取作者信息
        if(blogId != null && blogId< 1){
            return null;
        }
        UserInfo authorInfoByBlogId = userService.getAuthorInfoByBlogId(blogId);
        authorInfoByBlogId.setPassword("");
        return authorInfoByBlogId;
    }
}
